#include <iostream>
#include <vector>

using namespace std;

//在有序表中进行二分查找，成功时返回结点的位置，失败时返回-1
//设表为递增

int binarySearch(const vector<int> &vec, int num)
{
    int low = 0;
    int mid = 0;
    int high = vec.size();
    while (low <= high)
    {
        mid = (low + high) / 2;
        //如果中间的数小于所求，把下限上移到mid+1
        if (vec[mid] < num)
            low = mid + 1;
        //如果中间的数大于所求，把上限上移到mid-1
        else if (vec[mid] > num)
            high = mid - 1;
        //否则返回坐标值
        else
            return mid;
    }
    //如果未找到返回-1
    return -1;
}

int main(int argc, char const *argv[])
{
    vector<int> v(50);
    int i = 1;
    for (auto &&ele : v)
    {
        ele = 2 * i - 1;
        i++;
    }

    int pos = binarySearch(v, 34);
    cout << "下标：" << pos << endl;
    cout << "元素：" << v[pos] << endl;
    return 0;
}
